package com.wisdytech.linkdcs.partLife.service.impl;

import com.wisdytech.linkdcs.partLife.model.Part;
import com.wisdytech.linkdcs.partLife.model.PartStorageRecord;
import com.wisdytech.common.bean.Filter;
import com.wisdytech.common.constants.CommonConstants;
import com.wisdytech.common.exception.ServiceException;
import com.wisdytech.common.handler.BaseService;
import com.wisdytech.common.utils.BaseUtil;
import com.wisdytech.linkdcs.partLife.dao.IPartDao;
import com.wisdytech.linkdcs.partLife.dao.IPartStorageRecordDao;
import com.wisdytech.linkdcs.partLife.dto.PartStorageSaveDTO;
import com.wisdytech.linkdcs.partLife.dto.PartStorageSearchDTO;
import com.wisdytech.linkdcs.partLife.service.IPartStorageRecordService;
import com.wisdytech.linkdcs.partLife.vo.PartStorageRecordVO;
import com.github.pagehelper.PageInfo;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chenbin
 * @since 2018-06-05
 */
@Service("iPartStorageRecordService")
@Transactional
public class PartStorageRecordServiceImpl extends BaseService<IPartStorageRecordDao, PartStorageRecord> implements IPartStorageRecordService {
    /**
     * 注入备件信息管理dao
     */
	@Resource
	private IPartDao iPartDao;
    /**
     * 注入备件信息管理dao
     */
	@Resource
	private IPartStorageRecordDao iPartStorageRecordDao;
	
	@Override
	public void savepartStorage(PartStorageSaveDTO partStorageSaveDTO,String userId) throws ParseException {
		//如果领料人为空，则判断为入库
		if(partStorageSaveDTO.getPickUser()==null || partStorageSaveDTO.getPickUser()=="") {
			Double storageNum = partStorageSaveDTO.getStorageNum();
			Part part = iPartDao.selectById(partStorageSaveDTO.getPartId());
			Double nowStock = part.getNowStock();
			if(nowStock==null){
                nowStock = 0.0;
            }
			Double newNowStock = nowStock + storageNum;
			part.setNowStock(newNowStock);
			//更改备件现有库存数量
			iPartDao.updateAllColumnById(part);
			//保存入库信息
			PartStorageRecord partStorageRecord = new PartStorageRecord();
			partStorageRecord.setPkId(BaseUtil.getUUID());
			partStorageRecord.setPartId(partStorageSaveDTO.getPartId());
			partStorageRecord.setRemark(partStorageSaveDTO.getRemark());
			partStorageRecord.setStorageNum(partStorageSaveDTO.getStorageNum());
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date date0 = df.parse(partStorageSaveDTO.getStorageTime());		
			partStorageRecord.setStorageTime(date0);
			partStorageRecord.setStorageType(CommonConstants.IN_STORAGE);
			partStorageRecord.setStorageUser(userId);
			iPartStorageRecordDao.insert(partStorageRecord);
		}else {
			Double storageNum = partStorageSaveDTO.getStorageNum();
			Part part = iPartDao.selectById(partStorageSaveDTO.getPartId());
			Double nowStock = part.getNowStock();
			Double newNowStock = nowStock - storageNum;
	        if (newNowStock<0) {
	        	throw new ServiceException("出库数量小于现有库存");
	        }
			part.setNowStock(newNowStock);
			//更改备件现有库存数量
			iPartDao.updateAllColumnById(part);
			//保存入库信息
			PartStorageRecord partStorageRecord = new PartStorageRecord();
			partStorageRecord.setPkId(BaseUtil.getUUID());
			partStorageRecord.setPartId(partStorageSaveDTO.getPartId());
			partStorageRecord.setPickUser(partStorageSaveDTO.getPickUser());
			partStorageRecord.setRemark(partStorageSaveDTO.getRemark());
			partStorageRecord.setStorageNum(partStorageSaveDTO.getStorageNum());
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date0 = new Date();
			if(StringUtils.isNotEmpty(partStorageSaveDTO.getStorageTime())){
                 date0 = df.parse(partStorageSaveDTO.getStorageTime());
            }
			partStorageRecord.setStorageTime(date0);
			partStorageRecord.setStorageType(CommonConstants.OUT_STORAGE);
			partStorageRecord.setStorageUser(userId);
			iPartStorageRecordDao.insert(partStorageRecord);
		}
		
	}

	@Override
	public PageInfo<PartStorageRecordVO> listPartStorageByFilter(PartStorageSearchDTO partStorageSearchDTO,
			Filter filter) {
        Map<String, Object> queryMap = new HashMap<String, Object>();
        queryMap.put("partCode", partStorageSearchDTO.getPartCode());
        queryMap.put("partName", partStorageSearchDTO.getPartName());
        queryMap.put("partSpecification", partStorageSearchDTO.getPartSpecification());
        queryMap.put("partType", partStorageSearchDTO.getPartType());
        queryMap.put("storageType", partStorageSearchDTO.getStorageType());
        queryMap.put("storageUser", partStorageSearchDTO.getStorageUser());
        queryMap.put("startTime", partStorageSearchDTO.getStartTime());
        queryMap.put("endTime", partStorageSearchDTO.getEndTime());
        queryMap.put("shortcutTime", partStorageSearchDTO.getShortcutTime());//快捷时间
        queryMap.put("keyPartFlag", partStorageSearchDTO.getKeyPartFlag());
        queryMap.put("quickWearFlag", partStorageSearchDTO.getQuickWearFlag());
        List<PartStorageRecordVO> lists =
                super.beforeList(IPartStorageRecordDao.class, filter).listPartStorageByFilter(queryMap);
        PageInfo<PartStorageRecordVO> partStoragePage=new PageInfo<>(lists);
        return partStoragePage;
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	

}
